home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / distutils / command / build_scripts.py < prev    next >
Encoding:
Python Source  |  2000-07-27  |  3.1 KB  |  99 lines

  1. """distutils.command.build_scripts
  2.  
  3. Implements the Distutils 'build_scripts' command."""
  4.  
  5. # created 2000/05/23, Bastian Kleineidam
  6.  
  7. __revision__ = "$Id: build_scripts.py,v 1.4 2000/07/27 02:13:20 gward Exp $"
  8.  
  9. import sys, os, re
  10. from distutils.core import Command
  11. from distutils.dep_util import newer
  12.  
  13. # check if Python is called on the first line with this expression
  14. first_line_re = re.compile(r'^#!.*python(\s+.*)?')
  15.  
  16. class build_scripts (Command):
  17.  
  18.     description = "\"build\" scripts (copy and fixup #! line)"
  19.  
  20.     user_options = [
  21.         ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  22.         ('force', 'f', "forcibly build everything (ignore file timestamps"),
  23.         ]
  24.  
  25.  
  26.     def initialize_options (self):
  27.         self.build_dir = None
  28.         self.scripts = None
  29.         self.force = None
  30.         self.outfiles = None
  31.  
  32.     def finalize_options (self):
  33.         self.set_undefined_options ('build',
  34.                                     ('build_scripts', 'build_dir'),
  35.                                     ('force', 'force'))
  36.         self.scripts = self.distribution.scripts
  37.  
  38.  
  39.     def run (self):
  40.         if not self.scripts:
  41.             return
  42.         self.copy_scripts()
  43.  
  44.         
  45.     def copy_scripts (self):
  46.         """Copy each script listed in 'self.scripts'; if it's marked as a
  47.         Python script in the Unix way (first line matches 'first_line_re',
  48.         ie. starts with "\#!" and contains "python"), then adjust the first
  49.         line to refer to the current Python interpreter as we copy.
  50.         """
  51.         outfiles = []
  52.         self.mkpath(self.build_dir)
  53.         for script in self.scripts:
  54.             adjust = 0
  55.             outfile = os.path.join(self.build_dir, os.path.basename(script))
  56.  
  57.             if not self.force and not newer(script, outfile):
  58.                 self.announce("not copying %s (output up-to-date)" % script)
  59.                 continue
  60.  
  61.             # Always open the file, but ignore failures in dry-run mode --
  62.             # that way, we'll get accurate feedback if we can read the
  63.             # script.
  64.             try:
  65.                 f = open(script, "r")
  66.             except IOError:
  67.                 if not self.dry_run:
  68.                     raise
  69.                 f = None
  70.             else:
  71.                 first_line = f.readline()
  72.                 if not first_line:
  73.                     self.warn("%s is an empty file (skipping)" % script)
  74.                     continue
  75.  
  76.                 match = first_line_re.match(first_line)
  77.                 if match:
  78.                     adjust = 1
  79.                     post_interp = match.group(1)
  80.  
  81.             if adjust:
  82.                 self.announce("copying and adjusting %s -> %s" %
  83.                               (script, self.build_dir))
  84.                 if not self.dry_run:
  85.                     outf = open(outfile, "w")
  86.                     outf.write("#!%s%s\n" % 
  87.                                (os.path.normpath(sys.executable), post_interp))
  88.                     outf.writelines(f.readlines())
  89.                     outf.close()
  90.                 if f:
  91.                     f.close()
  92.             else:
  93.                 f.close()
  94.                 self.copy_file(script, outfile)
  95.  
  96.     # copy_scripts ()
  97.  
  98. # class build_scripts
  99.